local addonName = "SuperSimpleHunterBar"
-- SuperSimpleHunterBarAutoShotProgressLeftHalfFrame
-- SuperSimpleHunterBarAutoShotProgressRightHalfFrame
local tooltip = CreateFrame("GameTooltip", addonName .. "Tooltip", UIParent, "GameTooltipTemplate")
tooltip:Hide()
local thrown = false


local sysmsg = function(msg)
  DEFAULT_CHAT_FRAME:AddMessage("|cFFAAD372" .. msg)
end

local defaults = {
  autoshotx = 0,
  autoshoty = -222,
  autoshotw = 200,
  autoshoth = 16,
  castx = 0,
  casty = -200,
  castw = 200,
  casth = 22,
  autoshotstyle = 1,
}

local autoshotBarColor = { 0.4, 0.8, 0.4, 1 }
local castBarColor = { 0.4, 0.8, 0.4, 1 }
local autoshotBarSwingColor = { 0.8, 0.4, 0.4, 1 }

local loadDefaults = function()
  for k, v in pairs(defaults) do
    if SuperSimpleHunterBarDB[k] == nil then
      SuperSimpleHunterBarDB[k] = v
    end
  end
end

local frame = CreateFrame("Frame", "SuperSimpleHunterBarFrame", UIParent)
frame:RegisterEvent("ADDON_LOADED")

local cacheSpells = function()
  frame.spells = {}
  local _, _, offset, num = GetSpellTabInfo(GetNumSpellTabs())
  for i = 1, offset + num do
    local name, rank, id = GetSpellName(i, 'spell')
    if id then
      local _, _, texture = GetSpellInfo(i, 'spell')
      frame.spells[id] = { name = name, rank = rank, texture = texture }
    end
  end
end

local getSpell = function(id)
  local spell = frame.spells[id]
  if not spell then
    cacheSpells()
    spell = frame.spells[id]
  end
  return spell
end

local watchingSpells = {
  ["多重射击"] = 1,
  ["稳固射击"] = 1,
  ["瞄准射击"] = 1,
  ["投掷"] = 1,
}

-- 创建autoshotFrame
local autoshotFrame = CreateFrame("Frame", addonName .. "AutoShotFrame", UIParent)
autoshotFrame:Hide()
autoshotFrame:SetFrameLevel(3)
autoshotFrame:SetWidth(200)
autoshotFrame:SetHeight(16)
autoshotFrame:SetPoint("CENTER", UIParent, "CENTER", 0, -222) -- 位置稍微下移以区分castFrame

-- 创建autoshotFrame的子框架用于展示自动射击进度
local autoshotProgressFrame = CreateFrame("StatusBar", addonName .. "AutoShotProgressFrame", autoshotFrame)
autoshotProgressFrame:SetPoint("TOPLEFT", autoshotFrame, "TOPLEFT", 1, -1)
autoshotProgressFrame:SetPoint("BOTTOMRIGHT", autoshotFrame, "BOTTOMRIGHT", -1, 1)
-- autoshotProgressFrame:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
-- 在 Lua 中，若要设置一个没有纹理的材质，可将状态条的纹理设为空字符串。
-- 在Lua和WoW的环境里，SetStatusBarTexture方法可使用多种纹理值，以下是一些常见且干净的纹理选项：
-- 1. "Interface\\TargetingFrame\\UI-StatusBar": 这是游戏里常用的状态条纹理，外观干净且简洁。
-- 2. "Interface\\Tooltips\\UI-Tooltip-Background": 工具提示背景纹理，颜色均一，适合追求简洁外观的需求。
-- 3. "Interface\\BUTTONS\\WHITE8X8": 一个8x8的白色纹理，能通过SetStatusBarColor方法调整颜色，从而实现纯色状态条。
-- 若要设置纯色纹理，可按如下方式使用 "Interface\\BUTTONS\\WHITE8X8"：
-- autoshotProgressFrame:SetStatusBarTexture("Interface\\BUTTONS\\WHITE8X8")
-- autoshotProgressFrame:SetStatusBarColor(0.4, 0.8, 0.4, 1)

autoshotProgressFrame:SetStatusBarTexture("Interface\\BUTTONS\\WHITE8X8")
autoshotProgressFrame:SetStatusBarColor(unpack(autoshotBarColor))
autoshotProgressFrame:SetMinMaxValues(0, 1)
autoshotProgressFrame:SetValue(0)
-- autoshotProgressFrame:Hide()

-- local autoShotProgressLeftHalfFrame = CreateFrame("StatusBar", addonName .. "AutoShotProgressLeftHalfFrame", autoshotFrame)
-- autoShotProgressLeftHalfFrame:SetPoint("TOPRIGHT", autoshotFrame, "TOP", 0, -1)
-- autoShotProgressLeftHalfFrame:SetPoint("BOTTOMLEFT", autoshotFrame, "BOTTOMLEFT", 1, 1)
-- autoShotProgressLeftHalfFrame:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
-- autoShotProgressLeftHalfFrame:SetStatusBarColor(unpack(autoshotBarColor))
-- autoShotProgressLeftHalfFrame:SetMinMaxValues(0, 1)
-- -- autoShotProgressLeftHalfFrame:SetMinMaxValues(1, 0)
-- autoShotProgressLeftHalfFrame:SetValue(0)
-- autoShotProgressLeftHalfFrame:Hide()

-- local autoShotProgressRightHalfFrame = CreateFrame("StatusBar", addonName .. "AutoShotProgressRightHalfFrame", autoshotFrame)
-- autoShotProgressRightHalfFrame:SetPoint("TOPLEFT", autoshotFrame, "TOP", 0, -1)
-- autoShotProgressRightHalfFrame:SetPoint("BOTTOMRIGHT", autoshotFrame, "BOTTOMRIGHT", -1, 1)
-- autoShotProgressRightHalfFrame:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
-- autoShotProgressRightHalfFrame:SetStatusBarColor(unpack(autoshotBarColor))
-- autoShotProgressRightHalfFrame:SetMinMaxValues(0, 1)
-- autoShotProgressRightHalfFrame:SetValue(0)
-- autoShotProgressRightHalfFrame:Hide()

local asbShow = function()
  -- if not SuperSimpleHunterBarDB.autoshotstyle or SuperSimpleHunterBarDB.autoshotstyle <= 10 then
  --   autoshotProgressFrame:Show()
  --   autoShotProgressLeftHalfFrame:Hide()
  --   autoShotProgressRightHalfFrame:Hide()
  -- else
  --   autoshotProgressFrame:Hide()
  --   autoShotProgressLeftHalfFrame:Show()
  --   autoShotProgressRightHalfFrame:Show()
  -- end

  autoshotFrame:Show()
end

local castFrame = CreateFrame("Frame", addonName .. "CastFrame", UIParent)
castFrame:Hide()
castFrame:SetFrameLevel(3)
castFrame:SetWidth(200)
castFrame:SetHeight(22)
castFrame:SetPoint("CENTER", UIParent, "CENTER", 0, -200)
-- 创建castFrame的子框架用于展示背景色
local castBackgroundFrame = CreateFrame("Frame", addonName .. "CastBackgroundFrame", castFrame)
castBackgroundFrame:SetAllPoints(castFrame)
castBackgroundFrame:SetBackdrop({
  bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
  tile = true,
  tileSize = 0
})
castBackgroundFrame:SetBackdropColor(0, 0, 0, 0.5)
-- 创建castFrame的子框架用于展示施法进度
local castProgressFrame = CreateFrame("StatusBar", addonName .. "CastProgressFrame", castFrame)
castProgressFrame:SetPoint("TOPLEFT", castFrame, "TOPLEFT", 1, -1)
castProgressFrame:SetPoint("BOTTOMRIGHT", castFrame, "BOTTOMRIGHT", -1, 1)
castProgressFrame:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
-- castProgressFrame:SetStatusBarColor(0, 0.7, 1, 1) -- 蓝色
-- 猎人职业颜色通常为 #AAD372，转换为 RGB 为 (0.6667, 0.8275, 0.4471)
-- 银色的 RGB 值通常为 (0.75, 0.75, 0.75)，这里将进度条颜色设置为银色
castProgressFrame:SetStatusBarColor(unpack(castBarColor))
castProgressFrame:SetMinMaxValues(0, 1)
castProgressFrame:SetValue(0)


-- 创建castTextFrame作为castFrame的子Frame
local castTextFrame = CreateFrame("Frame", addonName .. "CastTextFrame", castFrame)
castTextFrame:SetAllPoints(castFrame)

-- 重新设置icon、leftText、rightText的父级为castTextFrame
local icon = castTextFrame:CreateTexture(nil, "OVERLAY")
icon:SetWidth(22)
icon:SetHeight(22)
icon:SetPoint("LEFT", castTextFrame, "LEFT", -22, 0)
icon:SetTexture("Interface\\Icons\\INV_Weapon_Bow_01")

local leftText = castTextFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
leftText:SetPoint("LEFT", icon, "RIGHT", 5, 0)
leftText:SetTextColor(1, 1, 1, 0.9)

local rightText = castTextFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
rightText:SetPoint("RIGHT", castTextFrame, "RIGHT", -5, -1)
rightText:SetTextColor(1, 1, 1, 0.9)
-- 调整leftText的位置
leftText:SetPoint("LEFT", icon, "RIGHT", 5, 0)

local resetAutoshotProgress = function(show)
  if SuperSimpleHunterBarDB.autoshothide == true then
    return
  end

  local ttime = GetTime() -- 获取当前时间，用于计算施法时间
  if not autoshotFrame.casttime or GetTime() >= autoshotFrame.starttime + autoshotFrame.casttime then
    autoshotFrame.casttime = UnitRangedDamage("player")
  end
  if not autoshotFrame.starttime or (autoshotFrame.starttime and ttime - autoshotFrame.starttime > autoshotFrame.casttime - 0.5) then
    autoshotFrame.starttime = ttime - autoshotFrame.casttime + 0.5
  end

  if show then
    asbShow()
  end
end

castFrame.fail = function()
  castFrame:Hide()
  resetAutoshotProgress()
end
castFrame.success = function()
  castFrame:Hide()
  resetAutoshotProgress()
end
castFrame.start = function(spellName, rank, texture, casttime)
  if SuperSimpleHunterBarDB.casthide == true then
    return
  end
  icon:SetTexture(texture)
  if rank and rank ~= '' then
    leftText:SetText(spellName .. '(' .. rank .. ')')
  else
    leftText:SetText(spellName)
  end

  castFrame.starttime = GetTime()
  castFrame.casttime = casttime
  castFrame:Show()
end

castFrame:SetScript("OnUpdate", function()
  local ttime = GetTime()
  local past = math.floor((ttime - castFrame.starttime) * 10) / 10
  local maxtime = math.floor(castFrame.casttime * 10 + 0.5) / 10
  rightText:SetText(past .. ' / ' .. maxtime)
  local percent = (ttime - castFrame.starttime) / castFrame.casttime
  if percent > 1 then percent = 1 end
  castProgressFrame:SetValue(percent)
end)

-- 创建autoshotFrame的子框架用于展示背景色
local autoshotBackgroundFrame = CreateFrame("Frame", addonName .. "AutoShotBackgroundFrame", autoshotFrame)
autoshotBackgroundFrame:SetAllPoints(autoshotFrame)
autoshotBackgroundFrame:SetBackdrop({
  bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
  tile = true,
  tileSize = 0
})
autoshotBackgroundFrame:SetBackdropColor(0, 0, 0, 0.5)

-- 创建autoshotTextFrame作为autoshotFrame的子Frame
local autoshotTextFrame = CreateFrame("Frame", addonName .. "AutoShotTextFrame", autoshotFrame)
autoshotTextFrame:SetAllPoints(autoshotFrame)

-- 重新设置icon、leftText、rightText的父级为autoshotTextFrame
local autoshotIcon = autoshotTextFrame:CreateTexture(nil, "OVERLAY")
autoshotIcon:SetWidth(16)
autoshotIcon:SetHeight(16)
autoshotIcon:SetPoint("LEFT", autoshotTextFrame, "LEFT", -16, 0)
-- autoshotIcon:SetTexture("Interface\\Icons\\INV_Weapon_Bow_01")

local autoshotLeftText = autoshotTextFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
autoshotLeftText:SetPoint("LEFT", autoshotIcon, "RIGHT", 5, -1)
autoshotLeftText:SetTextColor(1, 1, 1, 0.9)

local autoshotRightText = autoshotTextFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
autoshotRightText:SetPoint("RIGHT", autoshotTextFrame, "RIGHT", -5, -1)
autoshotRightText:SetTextColor(1, 1, 1, 0.9)

autoshotFrame.fail = function()
  autoshotFrame:Hide()
end
autoshotFrame.success = function()
  autoshotFrame:Hide()
end
autoshotFrame.start = function()
  if SuperSimpleHunterBarDB.autoshothide == true then
    return
  end

  autoshotFrame.starttime = GetTime()
  autoshotFrame.casttime = UnitRangedDamage("player")
  asbShow()
end

local toggleAutoshotBarStyle = function()
  autoshotProgressFrame:ClearAllPoints()
  if not SuperSimpleHunterBarDB.autoshotstyle or SuperSimpleHunterBarDB.autoshotstyle <= 10 then
    -- 左右，不区分自动射击前摇阶段
    autoshotProgressFrame:SetPoint("TOPLEFT", autoshotFrame, "TOPLEFT", 1, -1)
    autoshotProgressFrame:SetPoint("BOTTOMRIGHT", autoshotFrame, "BOTTOMRIGHT", -1, 1)
    -- autoshotProgressFrame:Show()
    -- autoShotProgressLeftHalfFrame:Hide()
    -- autoShotProgressRightHalfFrame:Hide()
  else
    -- autoshotProgressFrame:Hide()
    -- autoShotProgressLeftHalfFrame:Show()
    -- autoShotProgressRightHalfFrame:Show()
    autoshotProgressFrame:SetPoint("CENTER", autoshotFrame, "CENTER", 1, -1)
    autoshotProgressFrame:SetHeight(autoshotFrame:GetHeight() - 2)
  end
end

local getPercent = function(ttime, diff, reverse)
  if not ttime then ttime = GetTime() end
  local past = ttime - autoshotFrame.starttime
  local cooldownTime = autoshotFrame.casttime - 0.5
  local inCooldown = past < cooldownTime
  local maxtime = autoshotFrame.casttime
  local percent
  if inCooldown then
    maxtime = cooldownTime
    percent = past / maxtime
    if percent > 1 then
      percent = 1
    end
  else
    percent = (past - cooldownTime) / 0.5
    if percent > 1 then
      percent = 1
    end
    if diff then
      percent = 1 - percent
    end
  end

  if reverse then
    percent = 1 - percent
  end

  return percent
end

autoshotFrame:SetScript("OnUpdate", function()
  if not autoshotFrame.starttime then
    return
  end
  local ttime = GetTime()
  local past = math.floor((ttime - autoshotFrame.starttime) * 10) / 10
  local maxtime = math.floor(autoshotFrame.casttime * 10 + 0.5) / 10
  if past >= maxtime then
    past = maxtime
  end
  autoshotRightText:SetText(past .. ' / ' .. maxtime)

  local percent = (ttime - autoshotFrame.starttime) / autoshotFrame.casttime
  if percent > 1 then
    percent = 1
  end

  local cooldownTime = autoshotFrame.casttime - 0.5
  local inCooldown = ttime - autoshotFrame.starttime < cooldownTime
  local updateBarColor = function()
    -- 处理变色
    if inCooldown then
      autoshotProgressFrame:SetStatusBarColor(unpack(autoshotBarColor))
    else
      autoshotProgressFrame:SetStatusBarColor(unpack(autoshotBarSwingColor))
    end
  end
  if SuperSimpleHunterBarDB.autoshotstyle == 1 then
    -- 左右，不区分自动射击前摇阶段
    autoshotProgressFrame:SetValue(percent)
    autoshotProgressFrame:SetStatusBarColor(unpack(autoshotBarColor))
  else
    updateBarColor()
    if SuperSimpleHunterBarDB.autoshotstyle == 2 then
      -- 左右，区分自动射击前摇阶段
      autoshotProgressFrame:SetValue(percent)
    elseif SuperSimpleHunterBarDB.autoshotstyle == 3 then
      -- 左右，左右
      autoshotProgressFrame:SetValue(getPercent(ttime, false, false))
    elseif SuperSimpleHunterBarDB.autoshotstyle == 4 then
      -- 左右，右左
      autoshotProgressFrame:SetValue(getPercent(ttime, true, false))
    elseif SuperSimpleHunterBarDB.autoshotstyle == 5 then
      -- 右左，右左
      autoshotProgressFrame:SetValue(getPercent(ttime, false, true))
    elseif SuperSimpleHunterBarDB.autoshotstyle == 6 then
      -- 右左，左右
      autoshotProgressFrame:SetValue(getPercent(ttime, true, true))
    elseif SuperSimpleHunterBarDB.autoshotstyle == 11 then
      -- 收缩扩张
      local p = getPercent(ttime, true, true)
      autoshotProgressFrame:SetValue(1)
      -- autoshotProgressFrame:SetValue(0.999)
      -- print('width:', (autoshotFrame:GetWidth() - 2) * p)
      autoshotProgressFrame:SetWidth((autoshotFrame:GetWidth() - 2) * p)
      -- autoShotProgressLeftHalfFrame:SetValue(p)
      -- autoShotProgressRightHalfFrame:SetValue(p)
    elseif SuperSimpleHunterBarDB.autoshotstyle == 12 then
      -- 扩张收缩
      local p = getPercent(ttime, true, false)
      autoshotProgressFrame:SetValue(1)
      -- autoshotProgressFrame:SetValue(0.999)
      autoshotProgressFrame:SetWidth((autoshotFrame:GetWidth() - 2) * p)
      -- autoShotProgressLeftHalfFrame:SetValue(p)
      -- autoShotProgressRightHalfFrame:SetValue(p)
    end
  end



  local texture = GetInventoryItemTexture("player", 0)
  if not thrown then
    -- 不是投掷才显示弹药图标
    if texture then
      autoshotIcon:SetTexture(texture)
    else
      autoshotIcon:SetTexture("Interface\\Icons\\INV_Misc_QuestionMark")
    end
  end

  if thrown then
    tooltip:SetOwner(UIParent, "ANCHOR_NONE")
    tooltip:SetInventoryItem("player", 18)
    for i = 3, 10 do
      local textObj = getglobal(tooltip:GetName() .. "TextLeft" .. i)
      if textObj then
        local text = textObj:GetText()
        if text and string.find(text, "耐久度") then
          autoshotLeftText:SetText(text)
          break
        end
      end
    end
  else
    if texture == nil then
      autoshotLeftText:SetText("弹药耗尽")
    else
      local count = GetInventoryItemCount("player", 0)
      autoshotLeftText:SetText("" .. count)
    end
  end
end)

-- 修改OnEvent脚本以处理自动射击事件
-- 注意：后续需要将原来的OnEvent脚本中的相关处理替换为以下内容
-- 这里暂时不修改原脚本，仅添加对autoshotFrame的处理逻辑参考
-- 在实际使用时，需要将以下逻辑合并到原OnEvent脚本中
-- 例如：
-- frame:SetScript("OnEvent", function(self, event, ...)
--   local arg1, arg2, arg3, arg4, arg5 = ...
--   -- 原有的事件处理逻辑
--   -- 新增autoshotFrame的处理逻辑
--   if event == "UNIT_CASTEVENT" then
--     if UnitIsUnit("player", arg1) then
--       if arg3 == "CAST" then
--         local spell = getSpell(arg4)
--         if spell and spell.name == '自动射击' then
--           autoshotFrame.success()
--         end
--       elseif arg3 == "START" then
--         local spell = getSpell(arg4)
--         if spell and spell.name == '自动射击' then
--           autoshotFrame.starttime = GetTime()
--           autoshotFrame.casttime = arg5 / 1000
--           autoshotFrame.start(spell.name, spell.rank, spell.texture)
--         end
--       elseif arg3 == "FAIL" then
--         local spell = getSpell(arg4)
--         if spell and spell.name == '自动射击' then
--           autoshotFrame.fail()
--         end
--       end
--     end
--   end
-- end)

local checkThrown = function()
  thrown = true -- 没武器也当投掷，不显示弹药图标
  if not GetInventoryItemLink("player", 18) then
    autoshotIcon:Hide()
    return
  end
  thrown = false
  -- tooltip:SetOwner(UIParent, "ANCHOR_NONE")
  -- tooltip:SetInventoryItem("player", 18)
  -- for i = 1, 6 do
  --   local textObj = getglobal(tooltip:GetName() .. "TextLeft" .. i)
  --   if textObj then
  --     local text = textObj:GetText()
  --     if text and string.find(text, "投掷") then
  --       thrown = true
  --       break
  --     end
  --   end
  -- end

  -- 找到了API :D
  local _, _, itemLink = string.find(GetInventoryItemLink("player", 18), "item:(%d+):%d+:%d+:%d+")
  local _, _, _, _, _, _, _, itemInfo = GetItemInfo(itemLink)
  thrown = itemInfo == "INVTYPE_THROWN"

  if thrown then
    autoshotIcon:Hide()
  else
    autoshotIcon:Show()
  end
end

frame:SetScript("OnEvent", function()
  if event == "UNIT_CASTEVENT" then
    if UnitIsUnit("player", arg1) then
      if arg3 == "CAST" then
        local spell = getSpell(arg4)
        if spell and spell.name == '自动射击' then
          -- print(not autoshotFrame.starttime, GetTime() > autoshotFrame.starttime + autoshotFrame.casttime - 0.1)
          if not autoshotFrame.starttime or GetTime() > autoshotFrame.starttime + autoshotFrame.casttime - 0.1 then
            autoshotFrame.start()
          end
        elseif spell and watchingSpells[spell.name] then
          castFrame.success()
        end
      elseif arg3 == "START" then
        local spell = getSpell(arg4)
        if spell and watchingSpells[spell.name] then
          castFrame.start(spell.name, spell.rank, spell.texture, arg5 / 1000)
        end
      elseif arg3 == "FAIL" then
        local spell = getSpell(arg4)
        if spell and watchingSpells[spell.name] then
          -- 要不要显示个失败的动画
          castFrame.fail()
        elseif spell and spell.name == '自动射击' then
          autoshotFrame.fail()
        end
      end
    end
  elseif event == "UNIT_INVENTORY_CHANGED" then
    if arg1 == "player" then
      checkThrown() -- 检查是否是投掷武器
    end
  elseif event == "PLAYER_ENTERING_WORLD" then
    cacheSpells()
    checkThrown() -- 检查是否是投掷武器
  elseif event == "START_AUTOREPEAT_SPELL" then
    resetAutoshotProgress(true)
  elseif event == "ADDON_LOADED" and arg1 == "SuperSimpleHunterBar" then
    if not GetPlayerBuffID or not CombatLogAdd or not SpellInfo then
      sysmsg("未检测到SuperWow， " .. addonName .. "无法工作")
      return
    end
    local _, enClass = UnitClass("player")
    if enClass ~= "HUNTER" then
      sysmsg("你不是猎人， " .. addonName .. "无法工作")
      return
    end

    frame:RegisterEvent("UNIT_CASTEVENT")
    frame:RegisterEvent("START_AUTOREPEAT_SPELL")
    frame:RegisterEvent("PLAYER_ENTERING_WORLD")
    frame:RegisterEvent("UNIT_INVENTORY_CHANGED")

    SuperSimpleHunterBarDB = SuperSimpleHunterBarDB or {}
    loadDefaults()

    local db = SuperSimpleHunterBarDB
    autoshotFrame:SetPoint("CENTER", UIParent, "CENTER", db.autoshotx, db.autoshoty)
    autoshotFrame:SetWidth(db.autoshotw)
    autoshotFrame:SetHeight(db.autoshoth)
    autoshotIcon:SetHeight(db.autoshoth)
    autoshotIcon:SetWidth(db.autoshoth)
    autoshotIcon:ClearAllPoints()
    autoshotIcon:SetPoint("LEFT", autoshotTextFrame, "LEFT", -db.autoshoth, 0)
    castFrame:SetPoint("CENTER", UIParent, "CENTER", db.castx, db.casty)
    castFrame:SetWidth(db.castw)
    castFrame:SetHeight(db.casth)
    icon:SetHeight(db.casth)
    icon:ClearAllPoints()
    icon:SetPoint("LEFT", castFrame, "LEFT", -db.casth, 0)

    toggleAutoshotBarStyle()
  end
end)

local showHelp = function()
  sysmsg("SuperSimpleHunterBar帮助信息:")
  sysmsg("自动射击宽度: /sshbar abar w 200")
  sysmsg("自动射击高度: /sshbar abar h 16")
  sysmsg("自动射击横向位置: /sshbar abar x 0")
  sysmsg("自动射击纵向位置: /sshbar abar y -222")
  sysmsg("自动射击隐藏: /sshbar abar hide 1")
  sysmsg("自动射击显示: /sshbar abar hide 0")
  sysmsg("施法条宽度: /sshbar cbar w 200")
  sysmsg("施法条高度: /sshbar cbar h 22")
  sysmsg("施法条横向位置: /sshbar cbar x 0")
  sysmsg("施法条纵向位置: /sshbar cbar y -200")
  sysmsg("施法条隐藏: /sshbar cbar hide 1")
  sysmsg("施法条显示: /sshbar cbar hide 0")
  sysmsg("自动射击条样式: /sshbar asb style 1")
end

-- 注册 SlashCommand
SLASH_SSBAR1 = "/sshbar"
SLASH_SSBAR2 = "/ssbar" -- 兼容旧版命令
SlashCmdList["SSBAR"] = function(input)
  if not input then
    showHelp()
    return
  end

  input = string.lower(input)
  local args = {}
  -- 分割输入字符串
  for arg in string.gmatch(input, "%S+") do
    table.insert(args, arg)
  end

  local argsLength = table.getn(args)

  if argsLength < 3 then
    showHelp()
    return
  end

  if argsLength >= 3 then
    local target = args[1]
    local property = args[2]
    local value = tonumber(args[3])
    if not value then return end

    if target == "abar" then
      if property == "w" then
        autoshotFrame:SetWidth(value)
        SuperSimpleHunterBarDB.autoshotw = value
      elseif property == "h" then
        autoshotFrame:SetHeight(value)
        SuperSimpleHunterBarDB.autoshoth = value
        autoshotIcon:SetHeight(SuperSimpleHunterBarDB.autoshoth)
        autoshotIcon:SetWidth(SuperSimpleHunterBarDB.autoshoth)
        autoshotIcon:ClearAllPoints()
        autoshotIcon:SetPoint("LEFT", autoshotTextFrame, "LEFT", -SuperSimpleHunterBarDB.autoshoth, 0)
      elseif property == "x" then
        autoshotFrame:SetPoint("CENTER", UIParent, "CENTER", value, SuperSimpleHunterBarDB.autoshoty)
        SuperSimpleHunterBarDB.autoshotx = value
      elseif property == "y" then
        autoshotFrame:SetPoint("CENTER", UIParent, "CENTER", SuperSimpleHunterBarDB.autoshotx, value)
        SuperSimpleHunterBarDB.autoshoty = value
      elseif property == "hide" then
        local v = tonumber(value)
        if v == 1 or v == 0 then
          SuperSimpleHunterBarDB.autoshothide = v == 1
          if SuperSimpleHunterBarDB.autoshothide == true then
            autoshotFrame:Hide()
          end
        end
      end
    elseif target == "cbar" then
      if property == "w" then
        castFrame:SetWidth(value)
        SuperSimpleHunterBarDB.castw = value
      elseif property == "h" then
        castFrame:SetHeight(value)
        SuperSimpleHunterBarDB.casth = value
        icon:SetHeight(SuperSimpleHunterBarDB.casth)
        icon:ClearAllPoints()
        icon:SetPoint("LEFT", castFrame, "LEFT", -SuperSimpleHunterBarDB.casth, 0)
      elseif property == "x" then
        castFrame:SetPoint("CENTER", UIParent, "CENTER", value, SuperSimpleHunterBarDB.casty)
        SuperSimpleHunterBarDB.castx = value
      elseif property == "y" then
        castFrame:SetPoint("CENTER", UIParent, "CENTER", SuperSimpleHunterBarDB.castx, value)
        SuperSimpleHunterBarDB.casty = value
      elseif property == "hide" then
        local v = tonumber(value)
        if v == 1 or v == 0 then
          SuperSimpleHunterBarDB.casthide = v == 1
          if SuperSimpleHunterBarDB.casthide == true then
            castFrame:Hide()
          end
        end
      end
    elseif target == "asb" then
      if property == "style" then
        local v = tonumber(value)
        if not v then
          sysmsg("样式值必须是数字")
          return
        end
        if (v >= 1 and value <= 6) or (v >= 11 and v <= 12) then
          SuperSimpleHunterBarDB.autoshotstyle = value
          toggleAutoshotBarStyle()
          sysmsg("自动射击条样式已设置为: " .. value)
        end
      end
    end
  end
end
